<?php
include_once("batchDBManager.php");
include_once("videoConvert.php");
class VideoConvertManager
{
    var $cid=null;
    var $video_id=null;
    var $carrier_id=null;
    var $dbManger =null;
    var $dest_path =null;
    var $file_name =null;
    var $out_docomo_dir =null;
    var $out_au_dir =null;
    var $out_softbank_dir =null;
    var $flv_url =null;
   
    function init($para_cid,$para_video_id,$para_carrier_id,$para_flv_url) {
        $this->cid =$para_cid ; 
        $this->video_id =$para_video_id ; 
        $this->carrier_id =$para_carrier_id ;
        $this->flv_url =$para_flv_url ;
        
       	$this->dest_path = "/var/www/vhdocs/mobile/video/$this->video_id";
	$this->file_name ="$this->dest_path/FLV/$this->video_id.flv";
        $this->dbManger= new BatchDBManager();

	$this->out_docomo_dir="$this->dest_path/docomo";
	$this->out_au_dir="$this->dest_path/au";
	$this->out_softbank_dir="$this->dest_path/softbank";

    }

    function getState(){
	return $this->dbManger->getStatus($this->video_id,$this->carrier_id);
    }


    function makeFolders(){
        if (!file_exists($this->dest_path) ){
              mkdir($this->dest_path);
              mkdir("$this->dest_path/FLV");

              mkdir($this->out_docomo_dir);
              mkdir("$this->out_docomo_dir/3gp");
              mkdir("$this->out_docomo_dir/3gp/d");
              mkdir("$this->out_docomo_dir/3gp/s");
              mkdir("$this->out_docomo_dir/images");
              mkdir("$this->out_docomo_dir/images/thumbnail");
              mkdir("$this->out_docomo_dir/images/deco");
              mkdir("$this->out_docomo_dir/images/screensaver");

              mkdir($this->out_au_dir);
              mkdir("$this->out_au_dir/3gp");
              mkdir("$this->out_au_dir/images");
              mkdir("$this->out_au_dir/images/thumbnail");
              mkdir("$this->out_au_dir/images/deco");
              mkdir("$this->out_au_dir/images/screensaver");

              mkdir($this->out_softbank_dir);
              mkdir("$this->out_softbank_dir/3gp");
              mkdir("$this->out_softbank_dir/images");
              mkdir("$this->out_softbank_dir/images/thumbnail");
              mkdir("$this->out_softbank_dir/images/deco");
              mkdir("$this->out_softbank_dir/images/screensaver");
         
           
              shell_exec("chmod 755 $this->dest_path");
              shell_exec("chmod 755 $this->dest_path/FLV");
              shell_exec("chmod 755 $this->out_docomo_dir");
              shell_exec("chmod 755 $this->out_docomo_dir/3gp");
              shell_exec("chmod 755 $this->out_docomo_dir/3gp/d");
              shell_exec("chmod 755 $this->out_docomo_dir/3gp/s");
              shell_exec("chmod 755 $this->out_docomo_dir/images");
              shell_exec("chmod 755 $this->out_docomo_dir/images/thumbnail");
              shell_exec("chmod 755 $this->out_docomo_dir/images/deco");
              shell_exec("chmod 755 $this->out_docomo_dir/images/screensaver");

              shell_exec("chmod 755 $this->out_au_dir");
              shell_exec("chmod 755 $this->out_au_dir/3gp");            
              shell_exec("chmod 755 $this->out_au_dir/images");
              shell_exec("chmod 755 $this->out_au_dir/images/thumbnail");
              shell_exec("chmod 755 $this->out_au_dir/images/deco");
              shell_exec("chmod 755 $this->out_au_dir/images/screensaver");

              shell_exec("chmod 755 $this->out_softbank_dir");
              shell_exec("chmod 755 $this->out_softbank_dir/3gp");
              shell_exec("chmod 755 $this->out_softbank_dir/images");
              shell_exec("chmod 755 $this->out_softbank_dir/images/thumbnail");
              shell_exec("chmod 755 $this->out_softbank_dir/images/deco");
              shell_exec("chmod 755 $this->out_softbank_dir/images/screensaver");
	
 	}
    }

    function getFlvFile() {
        if ($this->flv_url != "") {
            if(!file_exists($this->file_name)) {
                $out=shell_exec("wget -O \"$this->file_name\" \"$this->flv_url\"");
                echo $out;
                if (!file_exists($this->file_name)) {
                    return false;
                } else {
                    return true;
                }
            } else {
                return true;
            }
        } else {
            return false;
        }
    }
    
    function doConvert(){
	$sta = $this->getState($this->video_id,$this->carrier_id);
	if($sta==2 || $sta==1){
	     $this->dbManger->updateExcnt($this->video_id,$this->carrier_id);
	} else  if($sta==9){
	     $this->dbManger-> updateStatus($this->video_id,$this->carrier_id,1);
	     $this->makeFolders();    
	     if ($this->getFlvFile()==true) {
	         $this->convertVedio();
	         $part_num =sizeof(scandir($this->get3gpdir()))-2; 
	         if ($part_num >0) {
	 		 $this->dbManger->updateExInfoSuccess($this->video_id,$this->carrier_id,$part_num);
	 	 } else {
	 		 $this->dbManger-> updateStatus($this->video_id,$this->carrier_id,9);
	 	 }
	      } 
	      else {
		  $this->dbManger->updateStatus($this->video_id,$this->carrier_id,9);
	      }
	 } else {
	      $this->dbManger->insertExInfoNoExists($this->video_id,$this->carrier_id,1);
	      $this->makeFolders();    
	      if ($this->getFlvFile()==true) {
	          $this->convertVedio();
	          $part_num =sizeof(scandir($this->get3gpdir()))-2; 
	          if ($part_num >0) {
	 	       echo "num:$part_num<br/>";	
		       $this->dbManger->updateExInfoSuccess($this->video_id,$this->carrier_id,$part_num);
	 	  }else {
	 	       $this->dbManger->updateStatus($this->video_id,$this->carrier_id,9);
	 	  }
	      } 
	      else {
		  $this->dbManger->updateStatus($this->video_id,$this->carrier_id,9);
	      }
	  }
     }

    function get3gpdir() {
    	if ($this->carrier_id == 1) {
    	    return "$this->out_docomo_dir/3gp/d";
    	}
    	else if ($this->carrier_id == 2) {
    	    return "$this->out_au_dir/3gp";
    	}
    	else if ($this->carrier_id == 3) {
    	    return "$this->out_softbank_dir/3gp";
    	} else {
    	    return "$this->dest_path/FLV";
    	}
    }
    
    function  convertVedio() {
         $converter = new VideoConverter();
         $converter->init($this->video_id,$this->carrier_id);
         $converter->doVonvert();
    }
}
?>
